import Vue from "vue"
import Vuex from "vuex"

Vue.use(Vuex);

const store = new Vuex.Store({

    state:{
        count:100,
        goods:[
            {id:100, name:"剃须刀",price:105.2, num:3},
            {id:101, name:"剃须刀",price:2.11, num:5},
            {id:102, name:"剃须刀",price:9.9, num:19},
            {id:103, name:"剃须刀",price:192.67, num:56},
            {id:104, name:"剃须刀",price:20.9, num:37},
            {id:105, name:"剃须刀",price:30.9, num:47},
            {id:106, name:"剃须刀",price:6.9, num:57},
            {id:107, name:"剃须刀",price:50.9, num:67},
        ],
        profile:{
            userId:"abcdef",
            pwd:"zxcvbnm6",
            name : "贾斯丁",
        }
    },
    //对象存放的都是函数，用来修改state里面的状态数据，进行“同步”通信操作
    mutations:{
        increment(state){
            state.count ++;
        },
        // addCountIncrement(state, payLoad){
        //     console.log(payLoad);
        //     state.count += payLoad.count;
        // },
        addCountIncrement(state, payLoad){
            //payLoad载核
            state.count += payLoad.count
        },
        decrement(state){
            state.count --;
        },
        addGood(state,payLoad){
            state.goods.push(payLoad.newGood)
        },
        updateProfile(state,payLoad){
            state.profile.userId = payLoad.newInfo.userId;
            state.profile.pwd = payLoad.newInfo.pwd;
            state.profile.name = payLoad.newInfo.name;
            //代码执行到这里的时候更新完毕
            payLoad.succeed();
        }

    },
    actions:{
        aupdateProfile(context,payLoad){
            setTimeout(function(){
                context.commit("updateProfile",payLoad)
            },3000)
        }
    },
    getters:{
        //方法繁琐 版本一
        // vipGoods(state){
        //     const goods = state.goods;
        //     const arr = [];
        //     for(let i = 0;i<goods.length;i++){
        //         if(goods[i].price>100){
        //             arr.push(goods[i]);
        //         }
        //     }
        //     return arr;
        // }
        // 版本二
        vipGoods(state){
        // return state.goods.filter((value,index,arr) =>{
        //     return value>100;
        // })
           return state.goods.filter(item => item.price >=100)
        },
        hotGoods(state,getters){
            return getters.vipGoods.filter(item => item.num>20)
        }
    },
    modules:{
        moduleA:{
            state:{
                name:"老罗",
                tel:"212111"
            },
            mutations:{
                changeName(state, payLoad){
                    state.name = payLoad;
                }
           },
            actions:{
                // 执行期上下文 context ==> store
                achangeName(context, payLoad){
                    setTimeout(() => {
                        console.log(context);
                        context.commit("changeName", payLoad);
                    }, 2000);
                }
            },
            getters:{
                addName(state){
                    return "锤子科技" + state.name;
                }
            }
        }
    }
});

export default store;